package com.yjc.rabbitmq.springboot_rabbitmq.controller;

import com.yjc.rabbitmq.springboot_rabbitmq.config.DelayedQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author shkstart
 * @create 2022-08-02 17:29
 */
@Slf4j
@RestController
public class SendMessageController {

    @Autowired
    RabbitTemplate rabbitTemplate;


    @GetMapping("/ttl/sendMsg/{msg}")
    public void sendMsg(@PathVariable("msg") String message) {
        log.info(message);
        rabbitTemplate.convertAndSend("X", "XA", "消息来自10秒的ttl队列" + message);
        rabbitTemplate.convertAndSend("X", "XB", "消息来自40秒的ttl队列" + message);
    }

    @GetMapping("/ttl/better/sendMsg/{message}/{time}")
    public void sendMsg(@PathVariable("message") String msg, @PathVariable String time) {
        log.info(msg);
        rabbitTemplate.convertAndSend("X", "XC", "消息来自" + time + "秒的ttl队列" + msg, (message) -> {
            message.getMessageProperties().setExpiration(time);
            return message;
        });
    }

    @GetMapping("/delayed/sendMsg/{message}/{time}")
    public void sendMsg(@PathVariable("message") String msg, @PathVariable("time") Integer time) {
        log.info(msg);
        rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE, DelayedQueueConfig.DELAYED_ROUTINGKEY, "延迟消息：" + msg, m -> {
            m.getMessageProperties().setDelay(time);
            return m;
        });
        System.out.println("--------------");
    }
}
